﻿#pragma once
#include <vector>
#include <string>
#include <iostream>


using namespace std;

inline unsigned long __stl_next_prime(unsigned long n)
{
	// Note: assumes long is at least 32 bits.
	static const int __stl_num_primes = 28;
	static const unsigned long __stl_prime_list[__stl_num_primes] = {
		53, 97, 193, 389, 769,
		1543, 3079, 6151, 12289, 24593,
		49157, 98317, 196613, 393241, 786433,
		1572869, 3145739, 6291469, 12582917, 25165843,
		50331653, 100663319, 201326611, 402653189, 805306457,
		1610612741, 3221225473, 4294967291
	};
	const unsigned long* first = __stl_prime_list;
	const unsigned long* last = __stl_prime_list + __stl_num_primes;
	const unsigned long* pos = lower_bound(first, last, n);
	return pos == last ? *(last - 1) : *pos;
}
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};


template<>
struct HashFunc<string>
{
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto e : key)
		{
			hash *= 31;
			hash += e;
		}

		return hash;
	}
};
namespace hash_bucket
{
	
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};

	template<class K, class T, class KeyOfT, class Hash>
	class HashTable;

	template <class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
	struct HBIterator
	{
		typedef HashTable<K, T, KeyOfT, Hash> HT;
		typedef HashNode<T>* PNode;
		typedef HBIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;

		HBIterator(PNode pNode = nullptr, const HT* pHt = nullptr)
			:_node(pNode)
			,_ht(pHt)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}

		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}

		Self& operator++()
		{
			
			if (_node->_next)
				_node = _node->_next;
			else
			{
				KeyOfT kot;
				Hash hash;
				size_t FullBucket = (hash(kot(_node->_data)) % _ht->_tables.size()) + 1;
				for (; FullBucket < _ht->_tables.size(); ++FullBucket)
				{
					_node = _ht->_tables[FullBucket];
					if (_node)
						break;
				}
				if (FullBucket == _ht->_tables.size())
					_node = nullptr;
			}
			return *this;
		}
		//后置++
		Self operator++(int)
		{
			Self tmp = *this;
			if (_node->_next)
				_node = _node->_next;
			else
			{
				KeyOfT kot;
				Hash hash;
				size_t FullBucket = (hash(kot(_node->_data)) % _ht->_tables.size()) + 1;
				for (; FullBucket < _ht->_tables.size(); ++FullBucket)
				{
					_node = _ht->_tables[FullBucket];
					if (_node)
						break;
				}
				if (FullBucket == _ht->_tables.size())
					_node = nullptr;
			}
			return tmp;
		}
	
		PNode _node;             
		const HT* _ht;         
	};

	template<class K, class T, class KeyOfT, class Hash>
	class HashTable
	{

		// 友元声明
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct HBIterator;

		typedef HashNode<T> Node;
	public:

		typedef HBIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;
		typedef HBIterator<K, T, const T&, const T*, KeyOfT, Hash> ConstIterator;


		Iterator Begin()
		{
			if (_n == 0)
				return End();
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
					return Iterator(cur, this);
			}
			return End();
		}

		Iterator End()
		{
			return Iterator(nullptr, this);
		}


		ConstIterator Begin() const
		{
			if (_n == 0)
				return End();
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
					return ConstIterator(cur, this);
			}
			return End();
		}

		ConstIterator End() const
		{
			return ConstIterator(nullptr, this);
		}


		HashTable()
		{
			_tables.resize(__stl_next_prime(0), nullptr);
		}


		// capacity
		size_t Size() const
		{
			return _n;
		}

		bool Empty() const
		{
			return _n == 0;
		}

		
		HashTable(const HashTable& ht)
		{
			_tables.resize(ht._tables.size());
			for (size_t i = 0; i < ht._tables.size(); i++)
			{
				Node* cur = ht._tables[i];
				while (cur)
				{
					Node* newnode = new Node(cur->_data);
					newnode->_next = _tables[i];
					_tables[i] = newnode;
					cur = cur->_next;
				}
			}
		}
		/*HashTable& operator=(const HashTable& ht)
		{
			if (this != &ht) 
			{  
				_tables.clear();  
				HashTable tmp(ht);
				_tables.swap(tmp._tables);
				_n = tmp._n;

			}
			return *this;

		}*/

		HashTable& operator=( HashTable ht)
		{
			if (this != &ht)
			{
				_tables.clear();
				_tables.swap(ht._tables);
				_n = ht._n;
			}
			return *this;

		}
		~HashTable()
		{
			Node* cur;
			for (size_t i = 0; i < _tables.size(); i++)
			{
				cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
		}


		size_t Count(const K& key)
		{
			if (Find(key) != End())
				return 1;
			else
				return 0;
		}

		size_t BucketCount() const 
		{
			size_t count = 0;
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
					count++;
			}
			return count;
		}
		
		size_t BucketSize(const K& key)
		{
			if (Find(key) != End())
			{
				size_t count = 0;
				Hash hash;
				size_t hashi = hash(key) % _tables.size();
				Node* cur = _tables[hashi];
				while (cur)
				{
					count++;
					cur = cur->_next;
				}
				return count;
			}
			else
				return 0;
		}
		pair<Iterator,bool> Insert(const T& data)
		{
			KeyOfT kot;;
			Hash hash;
			Iterator it = Find(kot(data));
			if (it != End())
				return { it, false };
				
			if (_n == _tables.size())
			{
				vector<Node*> NewHT(__stl_next_prime(_tables.size()+1));
				
				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = hash(kot(cur->_data)) % NewHT.size();
						cur->_next = NewHT[hashi];
						NewHT[hashi] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(NewHT);
			}
			Node* newnode = new Node(data);
			size_t hashi = hash(kot(data)) % _tables.size();
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			_n++;
			return { Iterator(newnode, this), true};
		}

		
		Iterator Find(const K& key)
		{
			Hash hash;
			KeyOfT kot;
			size_t hashi = hash(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					return Iterator(cur, this);
				}

				cur = cur->_next;
			}

			return End();
		}

		// 哈希桶中删除key的元素，删除成功返回true，否则返回false
		bool Erase(const K& key)
		{
			Hash hash;
			KeyOfT kot;
			if ( Find(hash(key)) != End())
			{
				size_t hashi = hash(key) % _tables.size();
				Node* cur = _tables[hashi];
				while (cur)
				{
					Node* next = cur->_next;
					Node* prev = nullptr;
					if (kot(cur->_data) == key)
					{
						if (prev == nullptr)
						{
							_tables[hashi] = cur->_next;
						}
						else
						{
							prev->_next = next;
						}
						delete cur;
						_n--;
						return true;
					}
					prev = cur;
					cur = next;
				}
				
			}
			else
				return false;
			
		}

	private:
		vector<Node*> _tables;  // 指针数组
		size_t _n = 0;			// 表中存储数据个数
	};
}

